'''
Created on Mar 8, 2011

@author: User
'''
import unittest
from cliques.rls.rls import RLS
from graph.classes.Graph import graph
import logging
import cliques.utils

class RlsTester(unittest.TestCase):

    def setUp(self):
        self.fixture = RLS()

    def tearDown(self):
        self.fixture = None

    def testRlsBugGraph2(self):
                
        test_graph = cliques.utils.import_graph("random-300-4800-2-125")
        test_graph = cliques.utils.convert_node_names_to_numbers(test_graph)
        start_node = 125
        k = 5
        algorithm = RLS(tie_breaking="Degree")
        algorithm.run(test_graph, k, start_node)
        
    def mtestRlsBugGraph(self):
        
        test_graph = cliques.utils.import_graph("bug-rls-degree.txt")
        for k in xrange(5,9):
            for node in test_graph.nodes():
                logging.info("Run testRlsBugGraph(%d,%s)" % (k,node) )
                self.fixture.run(test_graph, 5, node) 

    def testSimpleClique(self):
        ''' Test searching for a clique in a complete graph '''
        test_graph = graph()        
        nodes = [0,1,2,3]        
        test_graph.add_nodes(nodes)        
        test_graph.add_edge(0,1)
        test_graph.add_edge(0,2)
        test_graph.add_edge(0,3)
        test_graph.add_edge(1,2)
        test_graph.add_edge(1,3)
        test_graph.add_edge(2,3)

        self.fixture.run(test_graph,2,0)
        self.assertTrue(self.fixture.clique_found())
        
        self.fixture.run(test_graph,3,0)
        self.assertTrue(self.fixture.clique_found())        

        self.fixture.run(test_graph,4,0)
        self.assertTrue(self.fixture.clique_found())
        largest_clique = self.fixture.largest_clique
        self.assertEquals(4,len(largest_clique))
        self.assertEquals(set([0,1,2,3]), set(largest_clique))
        
    def testSmallImpossibleClique(self):
        test_graph = graph()        
        nodes = [0,1,2]        
        test_graph.add_nodes(nodes)        
        test_graph.add_edge(0,1)
        test_graph.add_edge(0,2)
        self.fixture.run(test_graph,3,0)
        self.assertFalse(self.fixture.clique_found())
        
    def testHarderClique(self):
        ''' Test  '''
        test_graph = graph()        
        nodes = [0,1,2,3,4]        
        test_graph.add_nodes(nodes)        
        test_graph.add_edge(0,1)
        test_graph.add_edge(0,2)
        test_graph.add_edge(0,3)
        test_graph.add_edge(1,2)
        test_graph.add_edge(1,3)
        test_graph.add_edge(2,3)
        test_graph.add_edge(4,1)
        test_graph.add_edge(4,2)

        self.fixture.run(test_graph,5,0)
        self.assertFalse(self.fixture.clique_found())

        for node in nodes:        
            self.fixture.run(test_graph,4,node)
            self.assertTrue(self.fixture.clique_found())        
            largest_clique = self.fixture.largest_clique
            self.assertEquals(4,len(largest_clique))
            self.assertEquals(set([0,1,2,3]), set(largest_clique))        


if __name__ == "__main__":
    #import sys;sys.argv = ['', 'Test.testName']
    logging.basicConfig(level=logging.INFO)    
    unittest.main()